package com.sense.cloud.coreservice.sdk.auth.utils;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.Objects;

public class DubboItfUtil {
  
  /**
   * @param objs.
   * @return str.
   */
  private static String paramsToString(Object... objs) {
    try {
      StringBuilder sb = new StringBuilder();
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(out);
      if (objs != null && objs.length > 0) {
        for (Object obj : objs) {
          oos.writeObject(obj);
          oos.flush();
          byte[] bs = out.toByteArray();
          sb.append(StringUtils.bytes2Hex(bs));
        }
      }
      return sb.toString();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "";
  }
  
  /**
   * @param random,objs.
   * @return str.
   */
  public static String getSignStr(String random, Object... objs) {
    String accesskey = SdkPropUtil.getAccessKey();
    String secret = SdkPropUtil.getSecret();
    String signStr = accesskey + secret + random + paramsToString(objs);
    return MD5Utils.md5(signStr);
  }
  
  /**
   * @param random,objs.
   * @return str.
   */
  public static String getSignture(Object... objs) {
    String accesskey = SdkPropUtil.getAccessKey();
    String secret = SdkPropUtil.getSecret();
    String random = StringUtils.getUniqeLowerCaseStringWithuuid();
    String signStr = accesskey + secret + random + paramsToString(objs);
    return random + MD5Utils.md5(signStr);
  }
  
  /**
   * 
   * @param accesskey.
   * @param random.
   * @param signStr.
   * @param objs.
   * @return bol.
   */
  public static boolean checkSignStr(String accesskey, String random, String signStr,
      Object... objs) {
    String secret = SdkPropUtil.getSecretFromMap(accesskey);
    String signStrOld = accesskey + secret + random + paramsToString(objs);
    String signStrNew = MD5Utils.md5(signStrOld);
    return Objects.deepEquals(signStrNew, signStr);
  }
  
  /**
   * 
   * @param accesskey.
   * @param random.
   * @param signStr.
   * @param objs.
   * @return bol.
   */
  public static boolean checkSignture(String accesskey, String signStr,
      Object... objs) {
    String secret = SdkPropUtil.getSecretFromMap(accesskey);
    String random = signStr.substring(0,32);
    String signStrOld = accesskey + secret + random + paramsToString(objs);
    String signStrNew = MD5Utils.md5(signStrOld);
    return Objects.deepEquals(signStrNew, signStr.substring(32, signStr.length()));
  }
  
  /**
   * dubbo 的签名 和 验证签名；. 原dubbo接口：test(String id);. 现dubbo接口：test(String
   * accesskey,String random,String signStr,String id); .
   */
  
  public static void main(String[] args) {
    Object[] objs = { "123", 123 };
    String s1 = getSignture(objs);
    System.out.println(s1);
    
    System.out.println(checkSignture(SdkPropUtil.getAccessKey(),s1, objs));
  }
  
  
}
